by Davis Chapman
In This Chapter
Thanks in part to the explosion in popularity of the Internet, more applications have the capability of communicating with other applications over networks, including the Internet. With Microsoft building networking capabilities into its operating systems, starting with Windows NT and Windows 95, these capabilities are becoming commonplace in all sorts of applications.
Some applications perform simple networking tasks, such as checking with a Web site to see whether there are any updates to the program and giving the user the option of updating his or her copy of the program. Some word processing applications format documents as Web pages, giving the user the option of loading the pages onto the Web server. Computer games enable the user to play against another person halfway around the world, instead of just competing against the game itself.
Applications can have any number of networking functions, and they all are built around the Winsock interface. If you know and understand how to program using the Winsock interface, and the MFC Winsock classes, this entire realm of application programming is open to you, expanding your programming options considerably.
Most applications that communicate over a network, whether its the Internet or a small office network, use the same principles and functionality to perform their communication. One application sits on a computer, waiting for another application to open a communication connection. This application is listening for this connection request, much as you listen for the phone to ring if you are expecting someone to call.
Meanwhile, another application, most likely on another computer (but not necessarily), tries to connect to the first application. This attempt to open a connection is similar to calling someone on the telephone. You dial the number and hope that the other person is listening for the phone on the other end. As the person making the call, you have to know the phone number of the person you are calling. If you dont know the phone number, you can look it up using the persons name. Likewise, the application trying to connect to the first application has to know the network location, or address, of the first application.
When the connection is made between the two applications, messages can pass back and forth between the two applications, much as you can talk to the person on the other end of the phone. This connection is a two-way communications channel, with both sides sending information, as seen in Figure 24.l.
Figure 24.1 The basic socket connection process.
Finally, when one or both sides have finished their sides of the conversation, the connection is closed, much as you hang up the phone when you have finished talking to the person you called. When the connection is closed from either side, the other side can detect it and close its side, just as you can tell if the person on the other end of the phone call has hung up on you or if youve been disconnected by some other means. This is a basic explanation of how network communications work between two or more applications.
Note:This is a basic description of how network communications work with the TCP/IP network protocol, which is the primary network protocol over the Internet. Many other network protocols use a subtle variation on this description. Other protocols, such as the UDP protocol, are more like radio broadcasts, where there is no connection between the two applications; one sends messages, and the other is responsible for making sure that it receives all of the messages.
The basic object used by applications to perform most network communications is called a socket. Sockets were first developed on UNIX at the University of California at Berkeley. Sockets were designed so that most network communications between applications could be performed in the same way that these same applications would read and write files. Sockets have progressed quite a bit since then, but the basics of how they work are still the same.
During the days of Windows 3.x, before networking was built into the Windows operating system, you could buy the network protocols required for network communications from numerous different companies. Each of these companies had a slightly different way that an application performed network communications. As a result, any applications that did perform network communications had a list of the different networking software that the application would work with. Many application developers were not happy with this situation. As a result, all the networking companies, including Microsoft, got together and developed the Winsock (Windows Sockets) API. This provided all application developers with a consistent API to perform all network communications, regardless of the networking software used.
When you want to read or write a file, you must use a file object to point to the file. A socket is similar; it is an object used to read and write messages that travel between applications.
Making a socket connection to another application does require a different set of information than opening a file. To open a file, you need to know the files name and location. To open a socket connection, you need to know the computer on which the other application is running and the port on which its listening. A port is like a phone extension, and the computer address is like the phone number. If you call someone at a large office building, you can dial the main office number, but then you need to specify the extension number, as shown in Figure 24.2. As with the phone number, you can look up the port number if you dont already know what it is, but this requires your computer to be configured with the information about which port the connecting application is listening on. If you specify the wrong computer address, or port number, you might get a connection to a different application; as with making the phone call, someone other than the person you called might answer the phone call. You also might not get an answer at all if there is no application listening at the other end.
Figure 24.2 Ports are used to route network communications to the correct application.
Note:Only one application can be listening on any specific port on a single computer. Although numerous applications can listen for connection requests on a single computer at the same time, each of these applications must listen on a different port.
When you build applications with MFC, you can use the MFC Winsock classes to add network communications capabilities with relative ease. The base class, CAsyncSocket, provides complete, event-driven socket communications. You can create your own descendant socket class that captures and responds to each of these events. The CSocket class is a descendant of the CAsyncSocket class, and encapsulates and simplifies some of the functionality of the base class.